define(function(require, exports, module) {

    function bindFollow(config) {
        $('.js-follow-user, .js-follow-user-del').on('click', function() {
            var _this = $(this);
            if (_this.is('.js-follow-user')) {
                var url = '/Ajax/User/follow';
                var i = 1;
                var empty = '';
                _this.removeClass('js-follow-user');
                _this.addClass('js-follow-user-del');
                var text = '已关注';
            } else {
                var url = '/Ajax/User/followDel';
                var i = -1
                var empty = '-empty';
                _this.removeClass('js-follow-user-del');
                _this.addClass('js-follow-user');
                var text = '加关注';
            }
            if (!_config.UserId) {
                showToLogin();
                return;
            }
            $.ajax({
                url: url,
                type: 'post',
                dataType: 'json',
                data: {userId: config.userId},
                success: function(d) {
                    if (d.error) {
                        alert(d.data);
                    } else {
                        var now = _this.find('span').text();
                        now = parseInt(now) + i;
                        _this.html('<i class="glyphicon glyphicon' + empty + '"></i> ' + text + ' (<span>' + now + '</span>)');

                    }
                }
            });
        });
    }

    function bindHello(c) {
        $('.js-hello').on('click', function() {
            if (!_config.UserId) {
                showToLogin();
                return;
            }
            var hello = $(getHelloHtml(c));
            hello.find('form').on('submit', function() {
                var userId = $(this).find('input[name="userId"]').val();
                var content = $(this).find('textarea[name="content"]').val();
                if (!content) {
                    alert('还是发句问候吧！');
                    return false;
                }
                if (content.length > 100) {
                    alert('简单的问候就行，在100字以内哦！');
                    return false
                }
                $.ajax({
                    url: '/Ajax/User/hello',
                    type: 'post',
                    data: {userId: userId, content: content},
                    dataType: 'json',
                    success: function(d) {
                        if (d.error) {
                            alert(d.data);
                        } else {
                            alert('你的问候已发送');
                            hello.modal('hide');
                        }
                    }
                });
                return false;
            });
            hello.on('hide.bs.modal', function(e) {
                $(e.target).remove();
            });
            hello.modal();
        });
    }

    function getHelloHtml(c) {
        var html ='<div class="modal fade"><form>';
        html += '<div class="modal-dialog">';
        html += '<div class="modal-content">';
        html += '<div class="modal-header">';
        html += '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
        html += '<h4 class="modal-title">给Ta打招呼</h4>';
        html += '</div>';
        html += '<div class="modal-body">';
        html += '<input name="userId" value="' + c.userId + '" type="hidden" />';
        html += '<textarea name="content" class="hello-textarea" placeholder="给句问候吧..."></textarea>';
        html += '</div>';
        html += '<div class="modal-footer">';
        html += '<button type="reset" data-dismiss="modal" aria-label="Close" class="btn btn-default">取消</button>';
        html += '<button type="submit" type="button" class="btn btn-primary">发送</button>';
        html += '</div>';
        html += '</div>';
        html += '</div>';
        html += '</form></div>';
        return html;
    }

    exports.init = function(c) {
        bindFollow(c);
        bindHello(c);
    }
});